package com.剑指offer;

import java.util.*;

public class _56删除链表中重复的结点
{
    public static void main(String[] args)
    {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(1);
        ListNode node3 = new ListNode(2);
        ListNode node4 = new ListNode(3);
        ListNode node5 = new ListNode(3);
        ListNode node6 = new ListNode(4);
        ListNode node7 = new ListNode(5);
        ListNode node8 = new ListNode(5);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        node6.next = node7;
        node7.next = node8;
        ListNode node = deleteDuplication(node1);
        System.out.println(node.next.val);
    }

    public static ListNode deleteDuplication(ListNode pHead)
    {
        ArrayList<Integer> list1 = new ArrayList<>();
        ArrayList<Integer> list2 = new ArrayList<>();
        ArrayList<Integer> list3 = new ArrayList<>();
        ArrayList<ListNode> list = new ArrayList<>();
        while (pHead != null)
        {
            list1.add(pHead.val);
            pHead = pHead.next;
        }
        for (int i = 0; i < list1.size() - 1; i++)
        {
            if (list1.get(i) == list1.get(i + 1))
            {
                list2.add(i);
                list2.add(i + 1);
            }
        }
        for (int i = 0; i < list1.size(); i++)
        {
            if (list2.contains(i))
            {
                continue;
            }
            else
            {
                list3.add(list1.get(i));
            }
        }
        if (list3.size() == 0)
        {
            return null;
        }
        for (int i = 0; i < list3.size(); i++)
        {
            list.add(new ListNode(list3.get(i)));
        }
        ListNode node = list.get(0);
        ListNode head = node;
        for (int i = 1; i < list.size(); i++)
        {
            node.next = list.get(i);
            node = node.next;
        }
        return head;
    }
}
